package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.data.bean.response.GameGiftDetailResponse
import com.lykj.llw.box.databinding.ActGameGiftBagDetailBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 礼包详情
 * ================================================
 *
 * @author gosha
 * @date 2023/1/5-16:53
 */
class GiftDetailActivity : BaseActivity() {
    lateinit var binding: ActGameGiftBagDetailBinding

    var id = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameGiftBagDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun initUI() {
        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initData() {
        binding.titleBar.tvTitle.text = "礼包详情"
        binding.titleBar.tvRightOption.visibility = View.GONE

        id = intent.getIntExtra("cid", id)

        obtainGiftDetail()
    }

    //获取礼包详情
    private fun obtainGiftDetail() {
        clearBodyParam()
        initBodyParam()
        bodyParams["id"] = id

        RetrofitManager.instance.obtainGiftDetail(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@GiftDetailActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                //解析gson
                var bean = GsonUtils.fromJson(
                    responseBody, GameGiftDetailResponse::class.java
                )

                setViewAttr(bean.data)
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let { LogUtils.e(msg) }
            }

        }, bodyParams)
    }

    //申请礼包
    private fun applyGiftBag() {
        clearBodyParam()
        initBodyParam()
        bodyParams["id"] = id
        RetrofitManager.instance.applyGiftBag(object : CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@GiftDetailActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                ToastUtils.showShort(this@GiftDetailActivity, "礼包申请成功")
                FlowBus.with<String>("update_card").post(lifecycleScope, "")
                obtainGiftDetail()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let { ToastUtils.showShort(this@GiftDetailActivity, msg) }
            }

        }, bodyParams)
    }

    private fun setViewAttr(data: GameGiftDetailResponse.DataDTO?) {
        binding.tvContent.text = data?.excerpt
        binding.tvDesc.text = data?.context
        binding.tvTime.text = "领取截止时间: ${data?.endTime}"

        when (data?.statusX) {
            0 -> {//申请中
                binding.tvBtnApplyNow.visibility = View.VISIBLE
                binding.cltGiftCode.visibility = View.GONE
            }

            1 -> {//已申请
                binding.tvBtnApplyNow.visibility = View.GONE
                binding.cltGiftCode.visibility = View.VISIBLE

                binding.tvGiftCode.text = "${data.card}"
            }
        }
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvCopy.clickWithTrigger {
            ClipboardUtils.copyText(binding.tvGiftCode.text)
            ToastUtils.showShort(this, "复制成功")
        }

        binding.tvBtnApplyNow.clickWithTrigger {
            //立即申请礼包
            applyGiftBag()
        }
    }

    override fun onDestroy() {
        removeDisposables(this@GiftDetailActivity)
        super.onDestroy()
    }
}